package com.testdemo2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;

public class demo2 {
    public static void main(String[] args) throws Exception {

        //1.读取配置文件中的信息
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("F:\\重头来过\\代码\\stu_reflect\\prop.properties");
        prop.load(fis);
        fis.close();
        System.out.println(prop);

        //2.获取全类名和方法名
        String classname = (String) prop.get("classname");
        String method = (String) prop.get("method");
        System.out.println(method);
        //3.利用反射创建对象并运行方法
        Class clazz = Class.forName(classname);
        //3.1创建对象
        Constructor constructor = clazz.getDeclaredConstructor(String.class,int.class);
        Object o = constructor.newInstance("zhangsan",2);
        //3.2调用方法
        Method method1 = clazz.getDeclaredMethod(method);
        method1.setAccessible(true);
        method1.invoke(o);
    }
}
